/***
 * Copyright (c) 2010 readyState Software Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package com.viethp.mt.mapoverlay;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.Log;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.viethp.mt.activities.AItemDetailsWithoutMap;
import com.viethp.mt.mapviewballoons.BalloonItemizedOverlay;

public class MyItemizedOverlay extends BalloonItemizedOverlay<OverlayItem> {

	private static final String ITEM_ID = "id";
	// private static final String ITEM_USER_ID = "user_id";
	// private static final String ITEM_USERNAME = "username";
	private static final String ITEM_TITLE = "title";
	private static final String ITEM_DESCRIPTION = "description";
	private static final String ITEM_EXP = "expired";
	private static final String ITEM_PRICE = "price";
	// private static final String ITEM_LON = "longitude";
	// private static final String ITEM_LAT = "latitude";
	private static final String ITEM_IMG_LINK = "imgLink";
	private static final String ITEM_MAX_BUDGET = "max_budget";
	private static final String ITEM_TOTAL_BIDDER = "total_bidder";

	private ArrayList<OverlayItem> m_overlays = new ArrayList<OverlayItem>();
	private Context c;

	public MyItemizedOverlay(Drawable defaultMarker, MapView mapView) {
		super(boundCenterBottom(defaultMarker), mapView);
		c = mapView.getContext();
		populate();
	}

	public void addOverlay(OverlayItem overlay) {
		m_overlays.add(overlay);
		populate();
	}

	@Override
	protected OverlayItem createItem(int i) {
		return m_overlays.get(i);
	}

	@Override
	public int size() {
		return m_overlays.size();
	}

	public void removeItem(int i) {
		if (i >= 0)
			m_overlays.remove(i);
		populate();
	}

	@Override
	protected boolean onBalloonTap(int index) {
		// Toast.makeText(c, "onBalloonTap for overlay index " + index +
		// "Title: "+m_overlays.get(index).getTitle(),
		// Toast.LENGTH_LONG).show();
		String[] temp;
		temp = m_overlays.get(index).getTitle().split("haphuocviet");
		String strtitle = temp[0];
		String strprice = temp[1];
		String img_url = temp[2];
		Log.d("avc", img_url);
		String exp = temp[3];
		String des = temp[4];
		String max_budget = temp[5];
		String total_bidder = temp[6];
		Log.d("avc", des);
		Intent i = new Intent(c, AItemDetailsWithoutMap.class);
		i.putExtra(ITEM_ID, m_overlays.get(index).getSnippet());
		i.putExtra(ITEM_TITLE, strtitle);
		i.putExtra(ITEM_DESCRIPTION, des);
		i.putExtra(ITEM_EXP, exp);
		i.putExtra(ITEM_PRICE, strprice);
		i.putExtra(ITEM_IMG_LINK, img_url);
		i.putExtra(ITEM_MAX_BUDGET, max_budget);
		i.putExtra(ITEM_TOTAL_BIDDER, total_bidder);

		c.startActivity(i);

		// CommonHelper.showToast(c, strtitle);
		return true;
	}

	@Override
	public void hideOtherBalloons(List<Overlay> overlays) {
		for (Overlay overlay : overlays) {
			((BalloonItemizedOverlay<?>) overlay).hideBalloon();
		}

	}

}
